home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Checkbox.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  8.3 KB  |  254 lines

  1. /*
  2.  *
  3. */
  4.  
  5. package symantec.itools.db.awt;
  6.  
  7. import java.applet.*;
  8. import symjava.sql.*;
  9. import symantec.itools.db.pro.*;
  10.  
  11. public class Checkbox extends java.awt.Checkbox implements ProjLink {
  12.  
  13.     private ProjBinder m_ProjBinder;
  14.  
  15.     private String  m_trueValue = new String("true");
  16.     private String  m_falseValue = new String("false");
  17.     private int     m_treatBlankAs = 0;
  18.     private boolean m_DynamicUpdate = false;
  19.     private boolean m_ignoreStateChange = true;
  20.  
  21.     /**
  22.      * Constructs a Checkbox with the specified label, no Checkbox group, and
  23.      * initialized to a false state.
  24.      * @param label the label on the Checkbox
  25.      */
  26.     public Checkbox(String label) {
  27.         super(label);
  28.     }
  29.  
  30.     public Checkbox() {
  31.         super();
  32.     }
  33.  
  34.     public void init(ProjBinder binder) {
  35.         m_ProjBinder = binder;
  36.         setEditable(m_ProjBinder);
  37.     }
  38.  
  39.     public void setTreatBlankAs(String blank)
  40.     {
  41.         if (new String(blank).toUpperCase().equals("DEFAULT"))
  42.         {
  43.             m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  44.         }
  45.             else if (new String(blank).toUpperCase().equals("NULL"))
  46.             {
  47.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  48.             }
  49.             else if (new String(blank).toUpperCase().equals("BLANK"))
  50.                 {
  51.                     m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  52.                 }
  53.     }
  54.  
  55.     public void setBinding(RelationView relView, String projection)
  56.     {
  57.         try
  58.         {
  59.             int projectionNumber = relView.findProjByName(projection);
  60.             relView.bindProj(projectionNumber, this);
  61.         }
  62.         catch (SQLException Ex)
  63.         {
  64.             raiseException(
  65.                 "SQLException from Checkbox.setBinding: "
  66.                 + Ex.getMessage());
  67.         }
  68.     }
  69.  
  70.     public void notifyDataChange(ProjBinder binder)
  71.     {
  72.         m_ignoreStateChange = true;
  73.         try {
  74.             if (m_trueValue.equals(binder.getStringValue()))
  75.             {
  76.                 setState(true);
  77.             }
  78.             else
  79.             {
  80.                 setState(false);
  81.             }
  82.         }
  83.         catch (SQLException Ex)
  84.         {
  85.             raiseException(
  86.                 "SQLException from Checkbox.notifyDataChange: "
  87.                 + Ex.getMessage());
  88.         }
  89.         catch (java.io.IOException Ex)
  90.         {
  91.             raiseException(
  92.                 "IOException from Checkbox.notifyDataChange: "
  93.                 + Ex.getMessage());
  94.         }
  95.     }
  96.  
  97.     public boolean notifySetData(ProjBinder binder) throws SQLException
  98.     {
  99.         return ( notifyStateChange() );
  100.     }
  101.  
  102.     boolean notifyStateChange()
  103.     {
  104.         if (!m_ignoreStateChange)
  105.         {
  106.             try
  107.             {
  108.                 if (m_ProjBinder != null && m_ProjBinder.isWritable())
  109.                 {
  110.                     if (getState())
  111.                     {
  112.                         m_ProjBinder.setValueFromString(m_trueValue, 0, m_treatBlankAs);
  113.                     }
  114.                     else
  115.                     {
  116.                         m_ProjBinder.setValueFromString(m_falseValue, 0, m_treatBlankAs);
  117.                     }
  118.                 }
  119.             }
  120.             catch (SQLException Ex)
  121.             {
  122.                 raiseException(
  123.                     "SQLException from Checkbox.notifyStateChange: "
  124.                     + Ex.getMessage());
  125.                 return false;
  126.             }
  127.             catch (java.io.IOException Ex)
  128.             {
  129.                 raiseException(
  130.                     "IOException from Checkbox.notifyStateChange: "
  131.                     + Ex.getMessage());
  132.                 return false;
  133.             }
  134.         }
  135.         return true;
  136.     }
  137.  
  138.     /**
  139.      * Handles the event. Returns true if the event is handled and
  140.      * should not be passed to the parent of this component. The default
  141.      * event handler calls some helper methods to make life easier
  142.      * on the programmer.
  143.      * @param evt the event
  144.      * @see #mouseEnter
  145.      * @see #mouseExit
  146.      * @see #mouseMove
  147.      * @see #mouseDown
  148.      * @see #mouseDrag
  149.      * @see #mouseUp
  150.      * @see #keyDown
  151.      * @see #action
  152.      */
  153.     public boolean handleEvent(java.awt.Event evt)
  154.     {
  155.         try
  156.         {
  157.             m_ignoreStateChange = false;
  158.             notifySetData(m_ProjBinder);
  159.         }
  160.         catch (SQLException Ex)
  161.         {
  162.             raiseException(
  163.                 "SQLException from Checkbox.handleEvent: "
  164.                 + Ex.getMessage());
  165.         }
  166.         return super.handleEvent(evt);
  167.     }
  168.  
  169.     public void setTrueValue(String trueValue) {
  170.         m_trueValue = new String(trueValue);
  171.         notifyDataChange(m_ProjBinder);
  172.     }
  173.  
  174.     public void setFalseValue(String falseValue) {
  175.         m_falseValue = new String(falseValue);
  176.         notifyDataChange(m_ProjBinder);
  177.     }
  178.  
  179.    void setEditable(ProjBinder binder)
  180.     {
  181.         //Disable change of state if binder is not writeable
  182.         disable();
  183.         try
  184.         {
  185.             if (binder != null)
  186.             {
  187.                 RelationView rv = binder.getRelationView();
  188.                 if (rv != null && (rv.getCurrentRecordState() != Record.RECSTATE_INVALID))
  189.                 {
  190.                     enable();
  191.                 }
  192.             }
  193.         }
  194.         catch (SQLException Ex)
  195.         {
  196.             raiseException(
  197.                 "SQLException from Checkbox.setEditable: "
  198.                 + Ex.getMessage());
  199.         }
  200.     }
  201.  
  202.     // When the control gets focus, save a copy of the text.
  203.     // When we lose focus, we use this copy to see if the data changed.
  204.     /**
  205.      * catch the got focus notification, save current data,
  206.      * and pass along the event.
  207.      *
  208.      * @param evt the got focus event being caught
  209.      * @param what the object that is getting focus
  210.      */
  211.     public boolean gotFocus(java.awt.Event evt, Object what)
  212.     {
  213.         return super.gotFocus(evt, what);
  214.     }
  215.  
  216.    //*************************************************************************//
  217.     //                                                                         //
  218.     // FUNCTION: raiseException                                                //
  219.     //                                                                         //
  220.     //  PURPOSE:                                                               //
  221.     //                                                                         //
  222.     //  PARAMETERS:                                                            //
  223.     //                                                                         //
  224.     //  RETURNS:                                                               //
  225.     //                                                                         //
  226.     // COMMENTS:                                                               //
  227.     //                                                                         //
  228.     //*************************************************************************//
  229.  
  230.     void raiseException(String text)
  231.     {
  232.         System.out.println(text);
  233.     }
  234.  
  235.     //*************************************************************************//
  236.     //                                                                         //
  237.     // FUNCTION: setDynamicUpdate                                              //
  238.     //                                                                         //
  239.     //  PURPOSE:                                                               //
  240.     //                                                                         //
  241.     // PARAMETERS:                                                             //
  242.     //                                                                         //
  243.     //  RETURNS:                                                               //
  244.     //                                                                         //
  245.     // COMMENTS:                                                               //
  246.     //                                                                         //
  247.     //*************************************************************************//
  248.  
  249.     public void setDynamicUpdate(boolean update)
  250.     {
  251.         m_DynamicUpdate = update;
  252.     }
  253. }
  254.